package com.hsy.linemoney.modules.exchange;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.CalculateService;

/**
 * Created by MartinKent on 2016/7/13.
 */
public class ExchangeConfirmDialog extends Dialog implements View.OnClickListener, DialogInterface.OnShowListener {

    private final OnCommitListener onCommitListener;
    protected View rootView;
    protected ImageView closeBtn;
    protected TextView fromAccount;
    protected TextView toAccount;
    protected TextView exchangeRate;
    protected TextView moneyTheOtherSideReceivedPrice;
    protected TextView moneyTheOtherSideReceivedUnit;
    protected EditText passwordInputView;
    protected TextView commit;
    protected TextView attendToExchangePrice;
    protected TextView attendToExchangeUnit;
    protected TextView costsPrice;
    protected TextView costsUnit;
    private View contentView;
    private OnShowListener mOnShowListener;

    private final ExchangeConfirmDialogData data;
    public static class ExchangeConfirmDialogData{

        private String exchangeAmount;
        private String exchangeAmountUnity;
        private String fromAccountId;
        private String toAccountId;
        private String exchangeRate;
        private String moneyTheOtherSideReceived;
        private String moneyTheOtherSideReceivedUnit;
        private String costs;
        private String costsUnit;

        public String getExchangeAmount() {
            return exchangeAmount;
        }

        public String getExchangeAmountUnity() {
            return exchangeAmountUnity;
        }

        public String getFromAccountId() {
            return fromAccountId;
        }

        public String getToAccountId() {
            return toAccountId;
        }

        public String getExchangeRate() {
            return exchangeRate;
        }

        public String getMoneyTheOtherSideReceived() {
            return moneyTheOtherSideReceived;
        }

        public String getMoneyTheOtherSideReceivedUnit() {
            return moneyTheOtherSideReceivedUnit;
        }

        public String getCosts() {
            return costs;
        }

        public String getCostsUnit() {
            return costsUnit;
        }

        public void setExchangeAmount(String exchangeAmount) {
            this.exchangeAmount = exchangeAmount;
        }

        public void setExchangeAmountUnity(String exchangeAmountUnity) {
            this.exchangeAmountUnity = exchangeAmountUnity;
        }

        public void setFromAccountId(String fromAccountId) {
            this.fromAccountId = fromAccountId;
        }

        public void setToAccountId(String toAccountId) {
            this.toAccountId = toAccountId;
        }

        public void setExchangeRate(String exchangeRate) {
            this.exchangeRate = exchangeRate;
        }

        public void setMoneyTheOtherSideReceived(String moneyTheOtherSideReceived) {
            this.moneyTheOtherSideReceived = moneyTheOtherSideReceived;
        }

        public void setMoneyTheOtherSideReceivedUnit(String moneyTheOtherSideReceivedUnit) {
            this.moneyTheOtherSideReceivedUnit = moneyTheOtherSideReceivedUnit;
        }

        public void setCosts(String costs) {
            this.costs = costs;
        }

        public void setCostsUnit(String costsUnit) {
            this.costsUnit = costsUnit;
        }
    }

    public ExchangeConfirmDialog(@NonNull Context context, @NonNull ExchangeConfirmDialogData data, @NonNull OnCommitListener onCommitListener) {
        super(context, R.style.exchange_confirm_pass_dialog_style);
        this.onCommitListener = onCommitListener;
        this.data = data;
        init(context);
    }

    private void init(Context context) {
        contentView = LayoutInflater.from(context).inflate(R.layout.exchange_confirm_pass_dialog, null);
        setContentView(contentView);
        initView(contentView);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.close_btn) {
            dismiss();
        } else if (view.getId() == R.id.commit) {
            dismiss();
            onCommitListener.onCommit(passwordInputView.getText().toString());
        }
    }

    private void initView(View rootView) {
        closeBtn = (ImageView) rootView.findViewById(R.id.close_btn);
        closeBtn.setOnClickListener(ExchangeConfirmDialog.this);
        fromAccount = (TextView) rootView.findViewById(R.id.from_account);
        toAccount = (TextView) rootView.findViewById(R.id.to_account);
        exchangeRate = (TextView) rootView.findViewById(R.id.exchange_rate);
        moneyTheOtherSideReceivedPrice = (TextView) rootView.findViewById(R.id.money_the_other_side_received_price);
        moneyTheOtherSideReceivedUnit = (TextView) rootView.findViewById(R.id.money_the_other_side_received_unit);
        passwordInputView = (EditText) rootView.findViewById(R.id.passwordInputView);
        commit = (TextView) rootView.findViewById(R.id.commit);
        commit.setOnClickListener(ExchangeConfirmDialog.this);
        attendToExchangePrice = (TextView) rootView.findViewById(R.id.attend_to_exchange_price);
        attendToExchangeUnit = (TextView) rootView.findViewById(R.id.attend_to_exchange_unit);
        costsPrice = (TextView) rootView.findViewById(R.id.costs_price);
        costsUnit = (TextView) rootView.findViewById(R.id.costs_unit);

        attendToExchangePrice.setText(data.getExchangeAmount());
        attendToExchangeUnit.setText(data.getExchangeAmountUnity());
        fromAccount.setText(data.getFromAccountId());
        toAccount.setText(data.getToAccountId());
        exchangeRate.setText(data.getExchangeRate());
        moneyTheOtherSideReceivedPrice.setText(data.getMoneyTheOtherSideReceived());
        moneyTheOtherSideReceivedUnit.setText(data.getMoneyTheOtherSideReceivedUnit());
        costsPrice.setText(data.getCosts());
        costsUnit.setText(data.getCostsUnit());
    }

    @Override
    public void setOnShowListener(OnShowListener listener) {
        this.mOnShowListener = listener;
    }

    @Override
    public final void onShow(DialogInterface dialog) {
        passwordInputView.requestFocus();

        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(passwordInputView, InputMethodManager.SHOW_FORCED);

        if (null != mOnShowListener) {
            mOnShowListener.onShow(dialog);
        }
    }

    public interface OnCommitListener {
        void onCommit(String pass);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setOnShowListener(this);
    }
}
